/**
 * Copyright (c) 2020 wuchenxi
 * walter-blog is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 * http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * <p>
 * <p>
 * Mulan Permissive Software License，Version 2
 * <p>
 * Mulan Permissive Software License，Version 2 (Mulan PSL v2)
 * January 2020 http://license.coscl.org.cn/MulanPSL2
 */
package com.walter.auth;

import com.walter.auth.config.user.BlogUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author wuchenxi
 * @date 2021-06-04 07:52:59
 */
@Slf4j
public class SecurityUtil {

    /**
     * 获取用户信息
     *
     * @return
     */
    public static BlogUser getBlogUser() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal == null) {
            log.info("用户未登录");
            return new BlogUser();
        }
        if (principal instanceof BlogUser) {
            return (BlogUser) principal;
        }

        log.info("用户未在系统中注册！principal={}", principal);
        return new BlogUser();
    }

    /**
     * 判断用户是否已经登录
     *
     * @return true:已登录；false：未登录
     */
    public static boolean authenticated() {
        return SecurityContextHolder.getContext().getAuthentication().isAuthenticated();
    }

}
